package com.androidlab.task1;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PositionLoggerActivity extends Activity {

	private Button startServiceButton;
	private Button stopServiceButton;
	private Button updateButton;
	private Button exitButton;
	private service serviceProxy;
	private ServiceConnection serviceConnection;
	boolean isBound = false;
	boolean isStopped = false;
	final Handler myHandler = new Handler();
	double latitude;
	double longitude;
	double distance;
	
    /** Called when the activity is first created. */
   
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeService();
        startServiceButton = (Button) findViewById(R.id.start_service_button);
        startServiceButton.setOnClickListener(startServiceButtonListener);
        stopServiceButton = (Button) findViewById(R.id.stop_service_button);
        stopServiceButton.setOnClickListener(stopServiceButtonListener);
        updateButton = (Button) findViewById(R.id.update_values_button);
        updateButton.setOnClickListener(updateButtonListener);
        exitButton = (Button) findViewById(R.id.exit_button);
        exitButton.setOnClickListener(exitButtonListener);
       
    }
    
    private void initializeService(){
    	Intent intent = new Intent();
    	intent.setClassName("com.androidlab.task1", "com.androidlab.task1.PositionLoggerService");
    	serviceConnection = new PositionLoggerServiceConnection();
    	bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);
    	
    }

    private void invokeService(){
    	final TextView latTv = (TextView)findViewById(R.id.latitude_text);
    	final TextView longTv = (TextView)findViewById(R.id.longitude_text);
    	final TextView distanceTv = (TextView)findViewById(R.id.distance_text);
    	if(serviceProxy == null){

    		latTv.setText("Service not ready");
    		longTv.setText("Service not ready");
    	}
    	else{
    		isBound = true;

    		try {
    			latitude = serviceProxy.getLat();
    			longitude = serviceProxy.getLong();
    			distance = serviceProxy.getDistance();

    			distanceTv.setText(""+distance);
    			latTv.setText(""+latitude);
    			longTv.setText(""+longitude);
    			
    		} 
    		
    		catch (RemoteException e) {
    			e.printStackTrace();
    		} 

//    		Thread myThread = new Thread() {
//    			public void run(){
//    				try {
//    					while(isBound){
//    						latitude = serviceProxy.getLat();
//    						longitude = serviceProxy.getLong();
//    						distance = serviceProxy.getDistance();
//    						TextView latTv = (TextView)findViewById(R.id.latitude_text);
//    						TextView longTv = (TextView)findViewById(R.id.longitude_text);
//    						TextView distanceTv = (TextView)findViewById(R.id.distance_text);
//    						distanceTv.setText(""+distance);
//    						latTv.setText(""+latitude);
//    						longTv.setText(""+longitude);
//    						Thread.sleep(1000);
//    					}
//    				} 
//    				catch (RemoteException e) {
//
//    					e.printStackTrace();
//    				} 
//    				catch (InterruptedException e) {
//    					e.printStackTrace();
//    				}
//    			}
//    		};
//    		myThread.start();
    	}
    }

    private void stopService(){
    	
    	if(isBound){
			unbindService(serviceConnection);
		 	TextView latTv = (TextView)findViewById(R.id.latitude_text);
	    	TextView longTv = (TextView)findViewById(R.id.longitude_text);
	      	TextView distanceTv = (TextView)findViewById(R.id.distance_text);
	    	distanceTv.setText("");
	    	latTv.setText("");
	    	longTv.setText("");
			isBound = false;
			isStopped = true;
			}
    }
    
    private OnClickListener stopServiceButtonListener = new OnClickListener() {
    	
    	public void onClick(View v){
    		
    		stopService();
    		
    	}
    };
    private OnClickListener updateButtonListener = new OnClickListener() {
    	public void onClick(View v){
    		// To be implemented
    	}
    };

    private OnClickListener startServiceButtonListener = new OnClickListener() {
  
    	public void onClick(View v){
    		if(isStopped){
    			initializeService();
    			isStopped = false;
    		}
    		invokeService();
    	}
    };
    private OnClickListener exitButtonListener = new OnClickListener() {
    	public void onClick(View v){
    		// To be implemented
    	}
    };
    
    class PositionLoggerServiceConnection implements ServiceConnection
    {
		public void onServiceConnected(ComponentName arg0, IBinder boundService) {
			serviceProxy = service.Stub.asInterface(boundService);
		}

		public void onServiceDisconnected(ComponentName arg0) {
			serviceProxy = null;
		}
    };
}